package com.hm.system.service.mongo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Service;

import com.hm.common.model.PageInfo;
import com.hm.common.util.RequestLogDef;
import com.hm.system.mongo.domain.log.QRequestPlanLog;
import com.hm.system.mongo.domain.log.RequestPlanLog;
import com.hm.system.mongo.vo.log.RequestPlanLogVo;
import com.hm.system.share.repository.mongo.log.RequestPlanLogRepository;
import com.hm.system.share.service.mongo.log.RequestPlanLogService;

/**
 * @author shishun.wang
 * @date 2016年5月13日 上午12:17:12
 * @version 1.0
 * @describe
 */
@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class RequestPlanLogServiceImpl implements RequestPlanLogService {

	@Autowired
	private RequestPlanLogRepository requestPlanLogRepository;

	@Override
	public String addRequestPlanLog(RequestPlanLog requestPlanLog) {
		return requestPlanLogRepository.save(requestPlanLog).getId();
	}

	@Override
	public PageInfo<RequestPlanLogVo> queryRequestPlanLog(String remoteIpAddress, String requestUrl, Long startTime,
			Long endTime, RequestLogDef requestLog, Integer pageNumber, Integer pageSize) {
		PageInfo<RequestPlanLogVo> pageInfo = new PageInfo<RequestPlanLogVo>(pageNumber, pageSize);

		QRequestPlanLog qRequestPlanLog = QRequestPlanLog.requestPlanLog;
		Pageable pageable = new QPageRequest(pageInfo.getPageNumber(), pageInfo.getPageSize(),
				qRequestPlanLog.createTime.desc());

		List<RequestPlanLogVo> requestPlanLogVos = new ArrayList<RequestPlanLogVo>();

		Page page = requestPlanLogRepository.findAll(pageable);
		List<RequestPlanLog> content = page.getContent();
		for (RequestPlanLog requestPlanLog : content) {
			RequestPlanLogVo requestPlanLogVo = new RequestPlanLogVo();
			{
				requestPlanLogVo.setId(requestPlanLog.getId());
				requestPlanLogVo.setTitle(requestPlanLog.getTitle());
				requestPlanLogVo.setContent(requestPlanLog.getContent());
				requestPlanLogVo.setRemoteIpAddress(requestPlanLog.getRemoteIpAddress());
				requestPlanLogVo.setRequestMethod(requestPlanLog.getRequestMethod());
				requestPlanLogVo.setRequestUrl(requestPlanLog.getRequestUrl());
				requestPlanLogVo.setRequestParameter(requestPlanLog.getRequestParameter());
				requestPlanLogVo.setCreateTime(requestPlanLog.getCreateTime());
			}

			requestPlanLogVos.add(requestPlanLogVo);
		}

		return pageInfo.execute(page, requestPlanLogVos);
	}
}
